package com.example.heimashop.view.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.heimashop.base.BaseActivity;
import com.example.heimashop.database.AddressDao;
import com.example.heimashop.databinding.ActivityAddressListBinding;
import com.example.heimashop.model.Address;
import com.example.heimashop.utils.ToastUtils;
import com.example.heimashop.view.adapter.AddressAdapter;

import java.util.List;

/**
 * 收货地址列表页面
 */
public class AddressListActivity extends BaseActivity<ActivityAddressListBinding> {

    private AddressAdapter adapter;
    private AddressDao addressDao;
    private ActivityResultLauncher<Intent> addAddressLauncher;
    private boolean isSelectMode = false; // 是否为选择模式

    @Override
    protected ActivityAddressListBinding getViewBinding() {
        return ActivityAddressListBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        // 获取选择模式标志
        isSelectMode = getIntent().getBooleanExtra("select_mode", false);

        // 设置标题栏返回按钮
        binding.toolbar.setNavigationOnClickListener(v -> finish());

        // 如果是选择模式，修改标题
        if (isSelectMode) {
            binding.toolbar.setTitle("选择收货地址");
        }

        // 初始化RecyclerView
        binding.rvAddress.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AddressAdapter();
        binding.rvAddress.setAdapter(adapter);

        // 初始化数据库
        addressDao = new AddressDao(this);

        // 注册Activity Result
        addAddressLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == RESULT_OK) {
                        // 刷新列表
                        loadAddressList();
                    }
                }
        );

        setupAdapter();
    }

    @Override
    protected void initData() {
        loadAddressList();
    }

    @Override
    protected void initListener() {
        // 添加地址按钮
        binding.btnAdd.setOnClickListener(v -> {
            Intent intent = new Intent(this, AddAddressActivity.class);
            addAddressLauncher.launch(intent);
        });
    }

    /**
     * 设置适配器监听
     */
    private void setupAdapter() {
        adapter.setListener(new AddressAdapter.OnAddressItemListener() {
            @Override
            public void onItemClick(Address address) {
                // 如果是选择模式，点击地址后返回地址ID
                if (isSelectMode) {
                    Intent result = new Intent();
                    result.putExtra("address_id", address.getId());
                    setResult(RESULT_OK, result);
                    finish();
                }
            }

            @Override
            public void onSetDefault(Address address) {
                addressDao.setDefaultAddress(address.getId());
                loadAddressList();
                ToastUtils.showShort(AddressListActivity.this, "已设为默认地址");
            }

            @Override
            public void onEdit(Address address) {
                Intent intent = new Intent(AddressListActivity.this, AddAddressActivity.class);
                intent.putExtra("address_id", address.getId());
                addAddressLauncher.launch(intent);
            }

            @Override
            public void onDelete(Address address) {
                int result = addressDao.deleteAddress(address.getId());
                if (result > 0) {
                    loadAddressList();
                    ToastUtils.showShort(AddressListActivity.this, "删除成功");
                } else {
                    ToastUtils.showShort(AddressListActivity.this, "删除失败");
                }
            }
        });
    }

    /**
     * 加载地址列表
     */
    private void loadAddressList() {
        List<Address> addresses = addressDao.getAllAddresses();
        if (addresses.isEmpty()) {
            binding.rvAddress.setVisibility(View.GONE);
            binding.llEmpty.setVisibility(View.VISIBLE);
        } else {
            binding.rvAddress.setVisibility(View.VISIBLE);
            binding.llEmpty.setVisibility(View.GONE);
            adapter.setData(addresses);
        }
    }
}
